﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Data.SqlTypes;
using System.Text.RegularExpressions;
using System.IO;

namespace Chaine
{
    public static class Util
    {
        public static bool IsError(this ModelState m)
        {
            return m != null && m.Errors != null && m.Errors.Count > 0;
        }

        public static string ErrorClass(this ModelState m, string @class)
        {
            return m.IsError() ? @class : "";
        }

        public static string ReplaceAll(this string s, IDictionary<string, string> template)
        {
            if (template == null)
                return string.Empty;
            foreach (var item in template)
            {
                s = s.Replace(item.Key, item.Value);
            }
            return s;
        }

        public static List<SelectListItem> ToSelectListItem(this List<string> stringList, string selectedValue = "")
        {
            if (stringList == null || stringList.Count == 0)
                return new List<SelectListItem>();
            selectedValue = selectedValue == null ? "" : selectedValue;//防止NULL
            var result = stringList.Select(s => new SelectListItem()
            {
                Text = string.IsNullOrEmpty(s) ? "-- Please select --" : s,
                Value = s,
                Selected = s.ToLower().Equals(selectedValue.ToLower())
            });
            if (result == null)
                return new List<SelectListItem>();
            return result.ToList();
        }

        public static HtmlHelper<T> HtmlHelperFor<T>(this HtmlHelper html, T model)
        {
            var newViewData = new ViewDataDictionary(html.ViewDataContainer.ViewData) { Model = model };
            ViewContext newViewContext = new ViewContext(html.ViewContext.Controller.ControllerContext, html.ViewContext.View, newViewData, html.ViewContext.TempData, html.ViewContext.Writer);
            var viewDataContainer = new ViewDataContainer(newViewContext.ViewData);
            return new HtmlHelper<T>(newViewContext, viewDataContainer, html.RouteCollection);
        }

        public static List<string> HoursList()
        {
            var result = new List<string>();
            for (int i = 0; i < 24; i++)
            {
                result.Add(i.ToString().PadLeft(2, '0'));
            }
            return result;
        }

        public static List<string> MinutesList()
        {
            var result = new List<string>();
            for (int i = 0; i < 60; i++)
            {
                result.Add(i.ToString().PadLeft(2, '0'));
            }
            return result;
        }

        public static List<string> CountryList = new List<string>() { "","Not Specified",
"Afghanistan",
"Albania",
"Algeria",
"American Samoa",
"Andorra",
"Angola",
"Anguilla",
"Antarctica",
"Antigua and Barbuda",
"Argentina",
"Armenia",
"Aruba",
"Australia",
"Austria",
"Azerbaijan",
"Bahamas",
"Bahrain",
"Bangladesh",
"Barbados",
"Belarus",
"Belgium",
"Belize",
"Benin",
"Bermuda",
"Bhutan",
"Bolivia",
"Bosnia and Herzegovina",
"Botswana",
"Bouvet Island",
"Brazil",
"British Indian Ocean Territory",
"Brunei",
"Bulgaria",
"Burkina Faso",
"Burundi",
"Cambodia",
"Cameroon",
"Canada",
"Cape Verde",
"Cayman Islands",
"Central African Republic",
"Chad",
"Chile",
"China",
"Christmas Island",
"Cocos (Keeling) Islands",
"Colombia",
"Comoros",
"Congo",
"Congo (DRC)",
"Cook Islands",
"Costa Rica",
"Cote d&amp;#39;Ivoire",
"Croatia (Hrvatska)",
"Cuba",
"Cyprus",
"Czech Republic",
"Denmark",
"Djibouti",
"Dominica",
"Dominican Republic",
"Dubai",
"East Timor",
"Ecuador",
"Egypt",
"El Salvador",
"EMEA",
"Equatorial Guinea",
"Eritrea",
"Estonia",
"Ethiopia",
"Falkland Islands (Islas Malvinas)",
"Faroe Islands",
"Fiji Islands",
"Finland",
"France",
"French Guiana",
"French Polynesia",
"French Southern and Antarctic Lands",
"Gabon",
"Gambia",
"Georgia",
"Germany",
"Ghana",
"Gibraltar",
"Greece",
"Greenland",
"Grenada",
"Guadeloupe",
"Guam",
"Guatemala",
"Guinea",
"GuineaBissau",
"Guyana",
"Haiti",
"Heard Island and McDonald Islands",
"Honduras",
"Hong Kong SAR",
"Hungary",
"Iceland",
"India",
"Indonesia",
"Iran",
"Iraq",
"Ireland",
"Israel",
"Italy",
"Jamaica",
"Japan",
"Jordan",
"Kazakhstan",
"Kenya",
"Kiribati",
"Korea",
"Kuwait",
"Kyrgyzstan",
"Laos",
"Latvia",
"Lebanon",
"Lesotho",
"Liberia",
"Libya",
"Liechtenstein",
"Lithuania",
"Luxembourg",
"Macau SAR",
"Macedonia Former Yugoslav Republic of",
"Madagascar",
"Malawi",
"Malaysia",
"Maldives",
"Mali",
"Malta",
"Marshall Islands",
"Martinique",
"Mauritania",
"Mauritius",
"Mayotte",
"Mexico",
"Micronesia",
"Middle East",
"Moldova",
"Monaco",
"Mongolia",
"Montserrat",
"Morocco",
"Mozambique",
"Myanmar",
"Namibia",
"Nauru",
"Nepal",
"Netherlands",
"Netherlands Antilles",
"New Caledonia",
"New Zealand",
"Nicaragua",
"Niger",
"Nigeria",
"Niue",
"Norfolk Island",
"North Korea",
"Northern Mariana Islands",
"Norway",
"Oman",
"Pakistan",
"Palau",
"Panama",
"Papua New Guinea",
"Paraguay",
"Peru",
"Philippines",
"Pitcairn Islands",
"Poland",
"Portugal",
"Puerto Rico",
"Qatar",
"Reunion",
"Romania",
"Russia",
"Rwanda",
"Samoa",
"San Marino",
"Saudi Arabia",
"Selangor",
"Senegal",
"Seychelles",
"Sierra Leone",
"Singapore",
"Slovakia",
"Slovenia",
"Solomon Islands",
"Somalia",
"Soo Tome and Principe",
"South Africa",
"South Georgia and the South Sandwich Islands",
"Spain",
"Sri Lanka",
"St. Helena",
"St. Kitts and Nevis",
"St. Lucia",
"St. Pierre and Miquelon",
"St. Vincent and the Grenadines",
"Sudan",
"Suriname",
"Svalbard and Jan Mayen",
"Swaziland",
"Sweden",
"Switzerland",
"Syria",
"Taiwan",
"Tajikistan",
"Tanzania",
"Thailand",
"Togo",
"Tokelau",
"Tonga",
"Trinidad and Tobago",
"Tunisia",
"Turkey",
"Turkmenistan",
"Turks and Caicos Islands",
"Tuvalu",
"U.A.E",
"UAE",
"Uganda",
"Ukraine",
"United Arab Emirates",
"United Kingdom",
"United States",
"United States Minor Outlying Islands",
"Uruguay",
"Uzbekistan",
"Vanuatu",
"Vatican City",
"Venezuela",
"Vietnam",
"Virgin Islands",
"Virgin Islands (British)",
"Wallis and Futuna",
"Yemen",
"Yugoslavia",
"Zambia",
"Zimbabwe",
"Others"};

        private static readonly string _randomchars = "abcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
        /// <summary>
        /// 获取随机密码
        /// </summary>
        /// <param name="length"></param>
        /// <returns></returns>
        public static string MakePassword(int length)
        {

            string tmpstr = "";
            int iRandNum;
            Random rnd = new Random();
            for (int i = 0; i < length; i++)
            {
                iRandNum = rnd.Next(_randomchars.Length);
                tmpstr += _randomchars[iRandNum];
            }
            return tmpstr;
        }

        public static void SaveFile(this HttpPostedFileWrapper file, string fileName)
        {
            var input = file.InputStream;
            using (Stream output = File.OpenWrite(fileName))
            {
                byte[] buffer = new byte[8192];
                int bytesRead;
                do
                {
                    bytesRead = input.Read(buffer, 0, buffer.Length);
                    output.Write(buffer, 0, bytesRead);
                } while (bytesRead == buffer.Length);
                file.InputStream.Close();
                file.InputStream.Dispose();
            }
        }
    }

    public class ViewDataContainer : System.Web.Mvc.IViewDataContainer
    {
        public ViewDataContainer(System.Web.Mvc.ViewDataDictionary viewData)
        {
            ViewData = viewData;
        }

        public System.Web.Mvc.ViewDataDictionary ViewData { get; set; }
    }


}